from flask_restful import Resource, reqparse,request
from ....Model.Models import Comment as CommentModel
from ....utils import timestamp,response
from flask_restful import reqparse, abort, Api, Resource, fields,\
    marshal_with
from flask_restful_swagger import swagger
'''
评论表
content = 内容
information_id = 资讯id
comment_user_id = 评论的用户id
create_user_id = 资讯发布者id
'''

# comment_poot_parser = reqparse.RequestParser()
# comment_poot_parser.add_argument('content', type=str)
# comment_poot_parser.add_argument('information_id', type=int)
# comment_poot_parser.add_argument('comment_user_id', type=int)
# comment_poot_parser.add_argument('create_user_id', type=int)


success = 1
notfond = -1
error = 0

class Commentlist(Resource):
    def __init__(self, db, auth=None):
        self.db = db
    def get(self):#获取列表
        try:
            return response(success , None, [each.as_dict() for each in self.db.session.query(CommentModel).all()])
        except Exception as er:
            return response(error,er,None)

    @swagger.operation(
      notes='添加评论',
      parameters=[
            {
            "name": "content",
            "description": "评论",
            "dataType": 'string',
            "paramType": "form"
          },{
            "name": "information_id",
            "description": "资讯id",
            "dataType": 'int',
            "paramType": "form"
          },{
            "name": "comment_user_id",
            "description": "评论人id",
            "dataType": 'int',
            "paramType": "form"
          },{
            "name": "create_user_id",
            "description": "资讯创建人id",
            "dataType": 'int',
            "paramType": "form"
          }
      ],
      responseMessages=[
          {
              "code": 201,
              "message": "Created. The URL of the created blueprint should " +
              "be in the Location header"
          },
          {
              "code": 405,
              "message": "Invalid input"
          }
      ])
    def post(self):#添加
        try:
            args = request.form
            self.db.session.add(
                    CommentModel(
                        content=args['content'],
                        information_id=args['information_id'],
                        comment_user_id =args['comment_user_id'],
                        create_user_id=args['create_user_id'],
                        update_at=timestamp(),
                        create_at=timestamp()
                    )
                )
            self.db.session.commit
            return response(success,None,None) #返回状态
        except Exception as er:
            return response(error,er,None)

class Comment(Resource):
    def __init__(self, db, auth=None):
        self.db = db

    def get(self,comment_id):#查
        try:
            return response(success,None, self.db.session.query(CommentModel).filter_by(id=comment_id).first().as_dict())
        except Exception as er:
            return response(error,er,None)
    def delete(self,comment_id):#删
        try:
            self.db.session.delete(
                self.db.session.query(CommentModel).filter_by(id=comment_id).first()
            )
            self.db.session.commit()
            return response(success,None,None)  #返回状态
        except Exception as er:
            return response(error,er,None)
       

    def put(self,comment_id):#改
        try:
            comment_obj = self.db.session.query(CommentModel).filter_by(id=comment_id).first()
            args = request.form
            if(args['content'] != None):
                comment_obj.content = args['content']
            if(args['information_id'] != None):
                comment_obj.information_id = args['information_id']
            if(args['comment_user_id'] != None):
                comment_obj.comment_user_id = args['comment_user_id']
            if(args['create_user_id'] != None):
                comment_obj.create_user_id = args['create_user_id']
            user_obj.update_at = timestamp()
            self.db.session.commit
            return response(success,None,None)  #返回状态
        except Exception as er:
            return response(error,er,None)
       

